<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="客户名称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable>
          <el-option label="启用" :value="0" />
          <el-option label="禁用" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getConsumerList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="deptList"
      style="width: 100%"
    >
      <el-table-column prop="nickName" label="客户名称" width="200"></el-table-column>
      <el-table-column prop="username" label="账号" width="200"></el-table-column>
      <el-table-column label="性别" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.sex === 0 ? 'success' : 'primary'">
            {{ scope.row.sex === 0 ? '女' : '男' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="会员状态" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.vip === 1 ? 'success' : 'info'">
            {{ scope.row.vip === 1 ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="money" label="金额" width="200"></el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 0 ? 'success' : 'danger'">
            {{ scope.row.status === 0 ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="mpn" label="手机号" width="200"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="客户名称" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入客户名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="账号" prop="username">
              <el-input v-model="form.username" placeholder="请输入账号" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" type="password" placeholder="请输入密码" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.mpn" placeholder="请输入联系电话" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别">
              <el-radio v-model="form.sex" :label="0">女</el-radio>
              <el-radio v-model="form.sex" :label="1">男</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio v-model="form.status" :label="0">启用</el-radio>
              <el-radio v-model="form.status" :label="1">禁用</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员状态">
              <el-radio v-model="form.vip" :label="0">否</el-radio>
              <el-radio v-model="form.vip" :label="1">是</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDept, getDept, delDept, addDept, updateDept, listDeptExcludeChild } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {addConsumer, detConsumer, listConsumer, updateConsumer} from "@/api/consumer/consumer";
import { parseTime } from '@/utils/ruoyi'

export default {
  name: "Dept",
  dicts: ['sys_normal_disable'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格数据
      deptList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ],
        phone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getConsumerList();
  },
  methods: {
    parseTime,
    getConsumerList() {
      this.loading = true;
      listConsumer(this.queryParams).then(response => {
        if (response.data && response.data.records) {
          this.deptList = response.data.records;
          console.log('获取到的数据:', this.deptList);
        } else {
          this.deptList = [];
          console.warn('未获取到数据或数据格式不正确');
        }
        this.loading = false;
      }).catch(error => {
        console.error('获取数据失败:', error);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getConsumerList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.parentId = row.deptId;
      }
      this.open = true;
      this.title = "添加部门";
      listDept().then(response => {
        this.deptOptions = this.handleTree(response.data, "deptId");
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      console.log(row)
      this.form = row;
      this.open = true;
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateConsumer(this.form).then(response => {
              if (response.code === 200) {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                });
                this.open = false;
                this.getConsumerList();
              } else {
                this.$message.error(response.msg || '修改失败');
              }
            }).catch(error => {
              this.$message.error('修改失败：' + error);
            });
          } else {
            addConsumer(this.form).then(response => {
              if (response.code === 200) {
                this.$message({
                  message: '新增成功',
                  type: 'success'
                });
                this.open = false;
                this.getConsumerList();
              } else {
                this.$message.error(response.msg || '新增失败');
              }
            }).catch(error => {
              this.$message.error('新增失败：' + error);
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.nickName + '"的数据项？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return detConsumer(row);
      }).then(response => {
        if (response.code === 200) {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getConsumerList();
        } else {
          this.$message.error(response.msg || '删除失败');
        }
      }).catch(error => {
        this.$message.error('删除失败：' + error);
      });
    }
  }
};
</script>
